Find validity of a string of parentheses¶
Write a python class to find validity of a string of parentheses:
‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘].
These brackets must be close in the correct order,
for example “()” and “()[]{}” are valid
but “[)”, “({[)]” and “{{{” are invalid.
class py_solution:
def is_valid_parenthese(self, str1):
stack = []
pchar = {"(": ")", "{": "}", "[": "]"}
for parenthese in str1:
if parenthese in pchar:
stack.append(parenthese)
elif len(stack) == 0 or pchar[stack.pop()] != parenthese:
return False
return len(stack) == 0
print(py_solution().is_valid_parenthese("(){}[]")) # True
print(py_solution().is_valid_parenthese("()[{)}")) # False
print(py_solution().is_valid_parenthese("()")) # True